<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Admin\Widget;
use Think\Controller;
class MenutypeWidget extends Controller {
	
	/*
	 * 根据用户ID获取用户名称
	 */
	public function showuser($userid=null){
	
		if(!empty($userid)){
			$user = D(C('TABLE_USER'));
			$userresult = $user->find($userid);
			return $userresult['username'];
		}
	
	}
	/*
	 * 显示资源类型
	 */
	public function showtype(){
		$str = null;
		$arr = array('1','2','3','4');
		foreach ($arr as $v){
			$str .= "<option value=".$v.">".menutype($v)."</option>";
		}
		return $str;
	}
	
	/*
	 * 获取选择的模板
	 */
	public function gettpl($tpl){
		switch ($tpl){
			case 'index2':
				echo "模板2";
				break;
			case 'index3':
				echo "模板3";
				break;
			case 'index4':
				echo "模板4";
				break;
			case 'index5':
				echo "模板5";
				break;
			case 'index6':
				echo "模板6";
				break;
			case 'index7':
				echo "模板7";
				break;	
			case 'index8':
				echo "模板8";
				break;
			case 'index9':
				echo "模板9";
				break;
			default:
				echo "模板1";
		}
	}
	
	/*
	 * 显示资源分类
	 */
	public function classtype(){
		$str = null;
		$arr = array('1','2','3','4','5');
		foreach ($arr as $v){
			$str .= "<option value=".$v.">".classtypename($v)."</option>";
		}
		return $str;
	}
    /*
     * 菜单列表(递归循环输出)
     * $len 递归层数
     * $id 菜单ID
     */
	public function showmenuinfo($id,$len=0){
		$submenu = D(C('TABLE_SUBMENU'));
		if($len>0){
			$result = $submenu->where(array('mparent'=>$id))->select();
		}else{
			$result = $submenu->where("mtypeid=%d and mparent=%d",array($id,0))->order('msort asc')->select();
		}
		foreach($result as $v){
			if($v['mparent']==0){
				if(!empty($v['binid'])){
					$lzi = "<div>来自：".$this->gettypema($v['binid'])."</div>";
				}else{
					$lzi = "";
				}
				/*1（企业） 2（政府）*/
				$p = gettypeaddres($v['classtype'],$v['id'],$v['mtypeid']);
				$preview = "<a class='preview' path='".$p."'>预览</a> / ";
				if(!empty($lzi)){
					$glian = $lzi;
				}else{
					$glian = "<span style=\"position:inherit; width:20%; margin:0px auto;\" v=\"".$v['id']."\">关联</span>".$lzi;
				}
				
			}
			/*if(session('userinfo')['type']==1){
				$deletestr = " / <a onclick=if(confirm('".C('DELETEMESSAGE')."')){submenudelete(\"".U('menudelete',array('id'=>$v['id']))."\")} style='cursor:pointer'>删除</a>";
			}*/
			$deletestr = " / <a onclick=if(confirm('".C('DELETEMESSAGE')."')){submenudelete(\"".U('menudelete',array('id'=>$v['id']))."\")} style='cursor:pointer'>删除</a>";
			$str .= "<tr class='tr-line'>";
			$str .= "<td>".$v['id']."</td>";
			$str .= "<td><div class='menutitle'><a href='".U('articlelist',array('id'=>$v['id'],'rootid'=>I('get.id')))."'>".str_repeat('—',$len).$v['mtitle']."</a></div><div class=\"postionshow\">".postionr($v['position'])."</div></td>";
			$str .= "<td class=\"guanlian\">".$glian."</td>";
			$str .= "<td>".$v['malias']."</td>";
			$str .= "<td>".menutype($v['mtype'])."</td>";
			$str .= "<td style='font-size:12px;'>".$v['mcrdate']."</td>";
			//$str .= "<td><a onclick=changestate('".U('Type/changestate',array('id'=>$v['id'],'state'=>$v['state'],'table'=>'submenu'))."')>".chagestateshow($v['state'])."</a></td>";
			$str .= "<td style='font-size:12px;'>".$v['msort']."</td>";
			$str .= "<td style='text-align:right;'>".$preview."<a href='".U('menuedit',array('id'=>$v['id'],'rootid'=>I('get.id')))."'>编辑</a>".$deletestr."</td>";
			$str .= "</tr>".$this->showmenuinfo($v['id'],$len+1);
		}
		return $str;
	}
	
	protected function gettypema($id){
		$type = D(C('TABLE_TYPE'));
		$res = $type->find($id);
		if(!empty($res)){
			$getthisname = $res['typename'];
		}
		if(!empty($getthisname)){
			return $getthisname;
		}else{
			return $this->gettypema2($this->gettypema1($id));
		}
	}
	
	protected function gettypema2($id){
		$type = D(C('TABLE_TYPE'));
		$res = $type->find($id);
		if(!empty($res)){
			return $res['typename'];
		}
	}
	
	protected function gettypema1($id){
		$stype = D(C('TABLE_SUBMENU'));
		$res1 = $stype->find($id);
		return $res1['mtypeid'];
	}
    /*
     * 获取菜单名称
     */
    public function getmenuname($id=null){
    	if(isset($id)){
    		$submenu = D(C('TABLE_SUBMENU'));
    		$result = $submenu->where(array('id'=>$id))->find();
    		return $result['mtitle'];
    	}
    }
    /*
     * 面包屑
     */
    public function nowhere($id=null){
    	if(isset($id)){
    		$str = "";
    		$submenu = D(C('TABLE_SUBMENU'));
    		$result = $submenu->field('id,mtitle,mparent')->where(array('id'=>$id))->find();
    		$mtitle = $result['mtitle'];
    		$mparent = $result['mparent'];
    		if($mparent){
    			$str .= $this->nowhere($mparent)." > <a href=".U('Index/articlelist',array('id'=>$result['id'],'rootid'=>$result['mtypeid'])).">".$mtitle."</a>";
    		}else{
    			$str = "<a href=".U('Index/articlelist',array('id'=>$this->getallid($result['id']))).">".$mtitle."</a>".$str;
    		}
    		return $str;
    	}
    }
    
    public function typename($mtypeid){
    	$type = D(C('TABLE_TYPE'));
    	$typeresult = $type->field('id,typename')->find($mtypeid);
    	return "<a style=\"color:#333;\" href=\"/Admin/Index/menulist/id/".$typeresult['id']."\">".$typeresult['typename']."</a> > ";
    }
    /*
     * 根据rootid获取所有子菜单ID号
     */
    protected function getallid($id=null){
    	if(isset($id)){
    		$n = $id.",";
    		$submenu = D(C('TABLE_SUBMENU'));
    		$where['mparent'] = array('in',$id);
    		$result = $submenu->field('id,mparent')->where($where)->select();
    		if(!empty($result)){
    			foreach ($result as $v){
    				$n .= $v['id'].",".$this->getallid($v['id']);
    				//$n .= $v['id'].",";
    			}
    		}
 
    		return $n;
    	}
    }
    /*
     * 资源类型
     */
    public function resourcetype($name,$typename,$v=null){
    	$str = null;
    	$str .= "<select name='".$name."' class='template form-control'>";
    	$str .= "<option value=''>请选择</option>";
    	foreach ($typename as $k=>$tname){
    		$k = $k+1;
    		$selected = null;
    		if(!empty($v)){
    			if($k==$v){
    				$selected = " selected";
    			}
    		}
    		$str .= "<option value='".$k."'".$selected.">".$tname."</option>";
    	}
    	$str .= "</select>";
    	return $str;
    }
        
    /*
     * 所属基金会
     */
    public function pjjh($id){
    	if(!empty($id)){
    		$d = D(C("TABLE_JIJINHUI"));
    		$res = $d->find($id);
    		echo $res['j_name'];
    	}
    	
    }
    
    /*
     * 资源列表下属分类
     */
    public function subresource($id){
    	$resource = D(C('TABLE_RESOURCE'));
    	$res = $resource->where(array('pid'=>$id))->select();
    	if($res){
    		return $res;
    	}else{
    		return false;
    	}
    }
    
    /*
     * 获取所属资源栏目
     */
    public function getresourcename($id){
    	if(!empty($id)){
    		$resource = D(C('TABLE_RESOURCE'));
    		$res = $resource->find($id);
    		if($res){
    			$tname = $res['typename'];
    			if($res['pid']==0){
    				$tnames = $tname;
    			}else{
    				$tnames = $this->getresourcename($res['pid']).' -> '.$tname;
    			}  			
    		}else{
    			$tnames = '栏目已删除，请重新归类';
    		}
    		echo $tnames;
    	}
    }

    
    
    
}
?>